package org.example.mytest.control;



import org.example.mytest.Service.AfterSaleService;
import org.example.mytest.emnu.Status;
import org.example.mytest.entity.Shouhoudan;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@CrossOrigin
@RestController
@RequestMapping("/after-sale")
public class AfterSaleController {
    private final AfterSaleService afterSaleService;

    public AfterSaleController(AfterSaleService afterSaleService) {
        this.afterSaleService = afterSaleService;
    }

    @PostMapping("/chuang")//创建售后单，接收前端送过来的表单数据
    public Shouhoudan create(@RequestBody Shouhoudan order) {

        return afterSaleService.createOrder(order);
    }

    @GetMapping("list")//查询数组中存储的表单数据
    public Shouhoudan[] getList()
    {
        return afterSaleService.getArrayinfo();
    }

    @GetMapping("/onlylist")//根据时间和商品名字选择性搜索表单信息，查询特定商品，特定时间的表单信息
    public Shouhoudan[] getList(
            @RequestParam(required = false) LocalDateTime time,
            @RequestParam(required = false) String shangpinname) {
        return afterSaleService.queryList(time, shangpinname);
    }

    @GetMapping("/{id}")//通过指定ID查询
    public ResponseEntity<?> getDetail(@PathVariable String id) {
        try {
            Shouhoudan order = afterSaleService.getDetailById(id);
            return ResponseEntity.ok(order);
        } catch (RuntimeException e) {

            return ResponseEntity.status(404).body(e.getMessage());
        }
    }

    @PutMapping("/audit/pass/{id}")//审核通过操作
    public ResponseEntity<String> passAudit(@PathVariable String id) {
        try {
            afterSaleService.audit(id, Status.AUDITED);
            return ResponseEntity.ok("审核通过");
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage());
        }
    }

    @PutMapping("/audit/reject/{id}")//审核拒绝操作
    public String rejectAudit(@PathVariable String id) {
        afterSaleService.audit(id, Status.REJECTED);
        return "审核拒绝";
    }

    @GetMapping("/overaudit")//查询已经审核的信息
    public ResponseEntity<?> searchAllAuditedByArray() {
        try {

            Shouhoudan[] auditedArray = afterSaleService.getoveraudits();
            return ResponseEntity.ok(auditedArray);
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage());
        }
    }

    @GetMapping("/rejectesaudit")//查询拒绝的信息
    public ResponseEntity<?> searchAllRejectByArray() {
        try {
            Shouhoudan[] rejectedauditedArray = afterSaleService.getrejectedaudits();
            return ResponseEntity.ok(rejectedauditedArray);
        } catch (RuntimeException e) {
            return ResponseEntity.status(404).body(e.getMessage());
        }
    }
}